package com.example.administrator.housepeasshop;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.housepeasshop.fragments.Classify;
import com.example.administrator.housepeasshop.fragments.Homepagefm;
import com.example.administrator.housepeasshop.fragments.Magic;
import com.example.administrator.housepeasshop.fragments.Me;
import com.example.administrator.housepeasshop.fragments.Shopcar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    // 底部菜单5个Linearlayout
    private LinearLayout ll_home;
    private LinearLayout ll_catogry;
    private LinearLayout ll_model;
    private LinearLayout ll_msg;
    private LinearLayout ll_me;
    // 底部菜单5个ImageView
    private ImageView iv_home;
    private ImageView iv_catogry;
    private ImageView iv_msg;
    private ImageView iv_model;
    private ImageView iv_me;
    // 底部菜单5个菜单标题
    private TextView tv_home;
    private TextView tv_catogry;
    private TextView tv_model;
    private TextView tv_msg;
    private TextView tv_me;

    // 5个Fragment
    private Fragment homeFragment;
    private Fragment caeogryFragment;
    private Fragment modelFragment;
    private Fragment msgFragment;
    private Fragment meFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
        initView();
        // 初始化底部按钮事件
        initEvent();
        // 初始化并设置当前Fragment
        initFragment(0);
    }

    private void initFragment(int index) {
        // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 隐藏所有Fragment
        hideFragment(transaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new Homepagefm();
                    transaction.add(R.id.fl_content, homeFragment);
                } else {
                    transaction.show(homeFragment);

                }
                break;
            case 1:
                if (caeogryFragment == null) {
                    caeogryFragment = new Classify();
                    transaction.add(R.id.fl_content, caeogryFragment);

                } else {
                    transaction.show(caeogryFragment);

                }

                break;
            case 2:
                if (modelFragment == null) {
                    modelFragment = new Magic();
                    transaction.add(R.id.fl_content, modelFragment);

                } else {
                    transaction.show(modelFragment);

                }

                break;
            case 3:
                if (msgFragment == null) {
                    msgFragment = new Shopcar();
                    transaction.add(R.id.fl_content, msgFragment);

                } else {
                    transaction.show(msgFragment);

                }

                break;
            case 4:
                if (meFragment == null) {
                    meFragment = new Me();
                    transaction.add(R.id.fl_content, meFragment);
                    transaction.show(meFragment);
                }
                break;
            default:
                break;
        }
        // 提交事务
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (caeogryFragment != null) {
            transaction.hide(caeogryFragment);
        }
        if (modelFragment != null) {
            transaction.hide(modelFragment);
        }
        if (msgFragment != null) {
            transaction.hide(msgFragment);
        }
        if (meFragment != null) {
            transaction.hide(meFragment);
        }
    }

    private void initEvent() {
        // 设置按钮监听
        ll_home.setOnClickListener(this);
        ll_catogry.setOnClickListener(this);
        ll_model.setOnClickListener(this);
        ll_msg.setOnClickListener(this);
        ll_me.setOnClickListener(this);
    }

    private void initView() {
        // 底部菜单4个Linearlayout
        this.ll_home = (LinearLayout) findViewById(R.id.ll_home);
        this.ll_catogry = (LinearLayout) findViewById(R.id.ll_catogry);
        this.ll_model = (LinearLayout) findViewById(R.id.ll_modle);
        this.ll_msg = (LinearLayout) findViewById(R.id.ll_msg);
        this.ll_me = (LinearLayout) findViewById(R.id.ll_me);
        // 底部菜单4个ImageView
        this.iv_home = (ImageView) findViewById(R.id.iv_home);
        this.iv_catogry = (ImageView) findViewById(R.id.iv_catogry);
        this.iv_model = (ImageView) findViewById(R.id.iv_modle);
        this.iv_msg = (ImageView) findViewById(R.id.iv_msg);
        this.iv_me = (ImageView) findViewById(R.id.iv_me);
        // 底部菜单4个菜单标题
        this.tv_home = (TextView) findViewById(R.id.tv_home);
        this.tv_catogry = (TextView) findViewById(R.id.tv_catogry);
        this.tv_model = (TextView) findViewById(R.id.tv_modle);
        this.tv_msg = (TextView) findViewById(R.id.tv_msg);
        this.tv_me = (TextView) findViewById(R.id.tv_me);

    }

    @Override
    public void onClick(View v) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
        restartBotton();
        // ImageView和TetxView置为绿色，页面随之跳转
        switch (v.getId()) {
            case R.id.ll_home:
                iv_home.setImageResource(R.drawable.icon_main_home_press);
                tv_home.setTextColor(Color.parseColor("#38BBBA"));
                initFragment(0);
                break;
            case R.id.ll_catogry:
                iv_catogry.setImageResource(R.drawable.icon_main_category_press);
                tv_catogry.setTextColor(Color.parseColor("#38BBBA"));
                initFragment(1);
                break;
            case R.id.ll_modle:
                iv_model.setImageResource(R.drawable.icon_main_magic_press);
                tv_model.setTextColor(Color.parseColor("#38BBBA"));
                initFragment(2);
                break;
            case R.id.ll_msg:
                iv_msg.setImageResource(R.drawable.icon_main_cart_press);
                tv_msg.setTextColor(Color.parseColor("#38BBBA"));
                initFragment(2);
                break;
            case R.id.ll_me:
                iv_me.setImageResource(R.drawable.icon_main_account_press);
                tv_me.setTextColor(Color.parseColor("#38BBBA"));
                initFragment(2);
                break;
            default:
                break;
        }
    }

    private void restartBotton() {
        // ImageView置为灰色
        iv_home.setImageResource(R.drawable.icon_main_home_nor);
        iv_catogry.setImageResource(R.drawable.icon_main_category);
        iv_model.setImageResource(R.drawable.icon_main_magic_nor);
        iv_msg.setImageResource(R.drawable.icon_main_cart_nor);
        iv_me.setImageResource(R.drawable.icon_main_account_nor);
        // TextView置为白色
        tv_home.setTextColor(Color.parseColor("#999999"));
        tv_catogry.setTextColor(Color.parseColor("#999999"));
        tv_model.setTextColor(Color.parseColor("#999999"));
        tv_msg.setTextColor(Color.parseColor("#999999"));
        tv_me.setTextColor(Color.parseColor("#999999"));
    }
}
